<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"remove","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":"false;","trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="spring5框架概述1.Spring 是轻量级的开源的 JavaEE 框架。 2.Spring 可以解决企业应用开发的复杂性。 3.Spring 有两个核心部分：IOC 和 Aop   IOC：控制反转，把创建对象过程交给 Spring 进行管理   Aop：面向切面，不修改源代码进行功能增强">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring5框架学习系列（一）">
<meta property="og:url" content="http://example.com/2021/09/04/spring5%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0(%E4%B8%80)/index.html">
<meta property="og:site_name" content="ADGai&#39;s Blog">
<meta property="og:description" content="spring5框架概述1.Spring 是轻量级的开源的 JavaEE 框架。 2.Spring 可以解决企业应用开发的复杂性。 3.Spring 有两个核心部分：IOC 和 Aop   IOC：控制反转，把创建对象过程交给 Spring 进行管理   Aop：面向切面，不修改源代码进行功能增强">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/12.png">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/13.png">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/16.png">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/image-20210406163000273.png">
<meta property="article:published_time" content="2021-09-04T14:32:51.517Z">
<meta property="article:modified_time" content="2021-10-26T09:21:58.693Z">
<meta property="article:author" content="AiLaodu">
<meta property="article:tag" content="Spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/12.png">


<link rel="canonical" href="http://example.com/2021/09/04/spring5%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0(%E4%B8%80)/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/09/04/spring5%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0(%E4%B8%80)/","path":"2021/09/04/spring5框架学习(一)/","title":"Spring5框架学习系列（一）"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Spring5框架学习系列（一） | ADGai's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">ADGai's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container">
  <div class="algolia-stats"><hr></div>
  <div class="algolia-hits"></div>
  <div class="algolia-pagination"></div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/04/spring5%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0(%E4%B8%80)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="AiLaodu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ADGai's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring5框架学习系列（一）
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-09-04 22:32:51" itemprop="dateCreated datePublished" datetime="2021-09-04T22:32:51+08:00">2021-09-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-10-26 17:21:58" itemprop="dateModified" datetime="2021-10-26T17:21:58+08:00">2021-10-26</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Spring/" itemprop="url" rel="index"><span itemprop="name">Spring</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="spring5框架概述"><a href="#spring5框架概述" class="headerlink" title="spring5框架概述"></a>spring5框架概述</h1><p>1.Spring 是轻量级的开源的 JavaEE 框架。</p>
<p>2.Spring 可以解决企业应用开发的复杂性。</p>
<p>3.Spring 有两个核心部分：IOC 和 Aop </p>
<ul>
<li><p>IOC：控制反转，把创建对象过程交给 Spring 进行管理 </p>
</li>
<li><p>Aop：面向切面，不修改源代码进行功能增强</p>
<span id="more"></span>

</li>
</ul>
<p>4.Spring 特点 </p>
<pre><code>    * 方便解耦，简化开发 
    * Aop 编程支持 
    * 方便程序测试 
    * 方便和其他框架进行整合 
    * 方便进行事务操作 
    * 降低 API 开发难度
</code></pre>
<h2 id="spring入门"><a href="#spring入门" class="headerlink" title="spring入门"></a>spring入门</h2><h3 id="1-下载Spring5"><a href="#1-下载Spring5" class="headerlink" title="1.下载Spring5"></a>1.下载Spring5</h3><p>下载地址：<a target="_blank" rel="noopener" href="https://repo.spring.io/release/org/springframework/spring/">https://repo.spring.io/release/org/springframework/spring/</a></p>
<h3 id="2-创建一个普通的Java项目"><a href="#2-创建一个普通的Java项目" class="headerlink" title="2.创建一个普通的Java项目"></a>2.创建一个普通的Java项目</h3><p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/12.png"></p>
<h3 id="3-导入Spring5相关的jar包"><a href="#3-导入Spring5相关的jar包" class="headerlink" title="3.导入Spring5相关的jar包"></a>3.导入Spring5相关的jar包</h3><p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/13.png"></p>
<h3 id="4-用Spring的方式来创建对象"><a href="#4-用Spring的方式来创建对象" class="headerlink" title="4.用Spring的方式来创建对象"></a>4.用Spring的方式来创建对象</h3><p>首先创建一个普通类，然后在这个类里面创建一个普通的方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;user的add方法........&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="5-创建-Spring-配置文件，在配置文件配置创建的对象"><a href="#5-创建-Spring-配置文件，在配置文件配置创建的对象" class="headerlink" title="5.创建 Spring 配置文件，在配置文件配置创建的对象"></a>5.创建 Spring 配置文件，在配置文件配置创建的对象</h3><p>Spring里创建对象有多种方式，可以使用配置文件，还可以使用注解方式，这里先简单的使用配置文件的方式来创建对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--配置User对象创建--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;user&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.User&quot;</span>&gt;&lt;/bean&gt;</span><br><span class="line">        <span class="comment">//注意这里的id是自己起的名字，对应后面的getBean方法中的var1</span></span><br><span class="line">        <span class="comment">//getBean(String var1, Class&lt;T&gt; var2) throws BeansException;</span></span><br></pre></td></tr></table></figure>

<h3 id="6-进行测试代码编写"><a href="#6-进行测试代码编写" class="headerlink" title="6.进行测试代码编写"></a>6.进行测试代码编写</h3><p>本代码只完成测试功能，在后续的工作中不会用到。建立一个测试类专门用来做测试。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">testSpring5</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testAdd</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//具体的测试代码，代码只用在测试中</span></span><br><span class="line">        <span class="comment">//1.加载Spring的配置文件</span></span><br><span class="line">        <span class="comment">//注意：由于类名有ClassPath即类路径，也就是src下所以可以直接写配置文件的名字，</span></span><br><span class="line">        <span class="comment">// 如果不在src下，则需要写下相对路径或者绝对路径（类名是FileSystemXmlApplication）</span></span><br><span class="line">        BeanFactory context =</span><br><span class="line">                <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;bean1.xml&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.获取创建的对象</span></span><br><span class="line">        User user = context.getBean(<span class="string">&quot;user&quot;</span>, User.class);</span><br><span class="line">        System.out.println(user);</span><br><span class="line">        user.add();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">输出结果为：</span><br><span class="line">    com.atguigu.Spring5.User@7c729a55</span><br><span class="line">    user的add方法........</span><br></pre></td></tr></table></figure>

<p>程序主要是通过Spring容器来访问容器中的Bean，==ApplicationContext是Spring容器最常用的接口，该接口有如下两个实现类==：</p>
<ul>
<li>ClassPathXmlApplicationContext: 从类加载路径下搜索配置文件，并根据配置文件来创建Spring容器。</li>
<li>FileSystemXmlApplicationContext: 从文件系统的相对路径或绝对路径下去搜索配置文件，并根据配置文件来创建Spring容器。</li>
</ul>
<h2 id="IOC容器"><a href="#IOC容器" class="headerlink" title="IOC容器"></a>IOC容器</h2><h3 id="Ⅰ-IOC底层的原理"><a href="#Ⅰ-IOC底层的原理" class="headerlink" title="Ⅰ.IOC底层的原理"></a>Ⅰ.IOC底层的原理</h3><h4 id="什么是IOC"><a href="#什么是IOC" class="headerlink" title="什么是IOC"></a>什么是IOC</h4><ul>
<li>控制反转，把对象创建和对象之间的调用过程，交给Spring进行管理。</li>
<li>使用Ioc的目的：为了降低耦合度</li>
<li>上文中的入门案例就是IOC实现</li>
</ul>
<p>先来看spring官网的关于IOC容器的解释：</p>
<blockquote>
<p>The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.</p>
</blockquote>
<p>大意就是：</p>
<ul>
<li>Spring IOC容器就是一个 <strong>org.springframework.context.ApplicationContext</strong> 的实例化对象。</li>
<li>容器负责实例化，配置以及装配一个bean。</li>
</ul>
<p><strong>从代码层面上看：spring容器就是一个实现了ApplicationContext接口的对象。</strong></p>
<p><strong>从功能层面来看：Spring 容器是 Spring 框架的核心，是用来管理对象的。容器将创建对象，把它们连接在一起，配置它们，并管理他们的整个生命周期从创建到销毁。</strong></p>
<h4 id="IOC底层原理"><a href="#IOC底层原理" class="headerlink" title="IOC底层原理"></a>IOC底层原理</h4><ul>
<li>xml注解、工厂模式、反射（得到class文件，操作类中的内容）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//工厂模式：</span></span><br><span class="line"><span class="comment">//使用工厂模式的目的是将耦合度降低到最低限度,例如在下面的例子中，当有很多service调用dao层时，dao层包发生改变所有的service都需要改，而有了工厂类以后只需要改变工厂类就可以，从而降低了代码的耦合度。</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserService</span></span>&#123;</span><br><span class="line">    excute()&#123;</span><br><span class="line">        UserDao dao = UserFactory.getDao();</span><br><span class="line">        dao.add();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserDao</span></span>&#123;</span><br><span class="line">    add()&#123;</span><br><span class="line">        .........;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserFactory</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="title">UserDao</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> UserDao();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>==IOC过程分析：==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一步：xml配置文件，配置创建的对象</span></span><br><span class="line">&lt;bean id=<span class="string">&quot;user&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.User&quot;</span>&gt;&lt;/bean&gt;</span><br><span class="line">    </span><br><span class="line"><span class="comment">//第二步：有service类和dao类，创建工厂类</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">UserFactory</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> userDao <span class="title">getDao</span><span class="params">()</span></span>&#123;</span><br><span class="line">            String classValue = <span class="class"><span class="keyword">class</span>属性值</span>;<span class="comment">//xml解析</span></span><br><span class="line">            <span class="comment">//通过反射创建对象</span></span><br><span class="line">            Class clazz = Class.forName(classValue);</span><br><span class="line">            <span class="keyword">return</span> (UserDao)calzz.newInstance();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; </span><br></pre></td></tr></table></figure>

<h4 id="IOC接口"><a href="#IOC接口" class="headerlink" title="IOC接口"></a>IOC接口</h4><ul>
<li><p>IOC思想基于IOC容器完成，IOC容器底层就是对象工厂</p>
</li>
<li><p>Spring提供了IOC容器实现的两种方式：（即两个接口）</p>
<p>（1）BeanFactory：IOC容器基本实现方式，时Spring内部的使用接口，不提供开发人员进行使用。</p>
<ul>
<li>加载配置文件的时候不会创建对象，获取对象的时候才去创建对象</li>
</ul>
<p>（2）ApplicationContext：BeanFactory接口的子接口，提供更多更强大的功能，一般由开发人员进行使用。</p>
<ul>
<li>加载配置文件的时候就会把在配置文件的对象进行创建。</li>
</ul>
</li>
<li><p>ApplicationContext接口有实现类</p>
<p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/16.png"></p>
</li>
</ul>
<ul>
<li>ClassPathXmlApplicationContext: 从类加载路径下搜索配置文件，并根据配置文件来创建Spring容器。</li>
<li>FileSystemXmlApplicationContext: 从文件系统的相对路径或绝对路径下去搜索配置文件，并根据配置文件来创建Spring容器。</li>
</ul>
<h3 id="Ⅱ-IOC操作Bean管理（概念）"><a href="#Ⅱ-IOC操作Bean管理（概念）" class="headerlink" title="Ⅱ.IOC操作Bean管理（概念）"></a>Ⅱ.IOC操作Bean管理（概念）</h3><h4 id="1-什么是Bean管理"><a href="#1-什么是Bean管理" class="headerlink" title="1.什么是Bean管理"></a>1.什么是Bean管理</h4><h5 id="（0）Bean管理指的是两个操作（1）（2）"><a href="#（0）Bean管理指的是两个操作（1）（2）" class="headerlink" title="（0）Bean管理指的是两个操作（1）（2）"></a>（0）Bean管理指的是两个操作（1）（2）</h5><h5 id="（1）Spring创建对象"><a href="#（1）Spring创建对象" class="headerlink" title="（1）Spring创建对象"></a>（1）Spring创建对象</h5><h5 id="（2）Spring注入属性"><a href="#（2）Spring注入属性" class="headerlink" title="（2）Spring注入属性"></a>（2）Spring注入属性</h5><h4 id="2-Bean管理操作有两种方式"><a href="#2-Bean管理操作有两种方式" class="headerlink" title="2.Bean管理操作有两种方式"></a>2.Bean管理操作有两种方式</h4><h5 id="（1）基于xml配置文件方式实现"><a href="#（1）基于xml配置文件方式实现" class="headerlink" title="（1）基于xml配置文件方式实现"></a>（1）基于xml配置文件方式实现</h5><h5 id="（2）基于注解方式实现"><a href="#（2）基于注解方式实现" class="headerlink" title="（2）基于注解方式实现"></a>（2）基于注解方式实现</h5><h3 id="Ⅲ-IOC操作Bean管理（基于xml方式）"><a href="#Ⅲ-IOC操作Bean管理（基于xml方式）" class="headerlink" title="Ⅲ.IOC操作Bean管理（基于xml方式）"></a>Ⅲ.IOC操作Bean管理（基于xml方式）</h3><h4 id="1-基于xml方式创建对象"><a href="#1-基于xml方式创建对象" class="headerlink" title="1.基于xml方式创建对象"></a>1.基于xml方式创建对象</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean id=<span class="string">&quot;user&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.User&quot;</span>&gt;&lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<p>（1）在 spring 配置文件中，使用 bean 标签，标签里面添加对应属性，就可以实现对象的创建。</p>
<p>（2）在 bean 标签有很多属性，介绍常用的属性。</p>
<ul>
<li>id 属性：唯一标识 user</li>
<li>class 属性：类全路径（包类路径）com.atguigu.Spring5.User</li>
<li>name属性：功能与id属性类似，区别在于name属性总可以加入特殊符号，但id属性不可以。用的很少</li>
</ul>
<p>（3）创建对象时候，默认也是执行无参数构造方法完成对象的创建。</p>
<h4 id="2-基于xml方式注入属性"><a href="#2-基于xml方式注入属性" class="headerlink" title="2.基于xml方式注入属性"></a>2.基于xml方式注入属性</h4><p>DI：依赖注入，就是注入属性。是IOC中的一种具体实现。注入属性需要在创建对象的基础之上完成。</p>
<p>==第一种注入方式：使用set方法进行注入。（bean标签+property标签加属性的值就可以完成注入）==</p>
<p>（1）创建类，定义属性和对应的set方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 演示使用set方法进行注入属性</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>&#123;</span><br><span class="line">    <span class="comment">//创建属性</span></span><br><span class="line">    <span class="keyword">private</span> String bname;</span><br><span class="line">    <span class="keyword">private</span> String bauthor;</span><br><span class="line">    <span class="comment">//创建属性对应的set方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBname</span><span class="params">(String bname)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.bname = bname;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBauthor</span><span class="params">(String bauthor)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.bauthor = bauthor;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（2）在Spring配置文件配置对象创建，配置属性注入。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">2</span> set方法注入属性--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;book&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.Book&quot;</span>&gt;</span><br><span class="line">        &lt;!--使用property完成属性注入</span><br><span class="line">         name：类里面属性名称</span><br><span class="line">            value：向属性注入的值--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;bname&quot;</span> value=<span class="string">&quot;易筋经&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;bauthor&quot;</span> value=<span class="string">&quot;达摩老祖&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>



<p>==第二种注入方式：使用有参数构造进行注入==</p>
<p>（1）创建类，定义属性，创建属性对应有参数构造方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用有参数构造注入</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Orders</span> </span>&#123;</span><br><span class="line">    <span class="comment">//属性</span></span><br><span class="line">    <span class="keyword">private</span> String oname;</span><br><span class="line">    <span class="keyword">private</span> String address;</span><br><span class="line">    <span class="comment">//有参数构造</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Orders</span><span class="params">(String oname,String address)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.oname = oname;</span><br><span class="line">        <span class="keyword">this</span>.address = address;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ordersTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(oname+<span class="string">&quot;::&quot;</span>+address);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（2）在 spring 配置文件中进行配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">3</span> 有参数构造注入属性--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;orders&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.Orders&quot;</span>&gt;</span><br><span class="line">        &lt;constructor-arg name=<span class="string">&quot;oname&quot;</span> value=<span class="string">&quot;电脑&quot;</span>&gt;&lt;/constructor-arg&gt;</span><br><span class="line">        &lt;constructor-arg name=<span class="string">&quot;address&quot;</span> value=<span class="string">&quot;China&quot;</span>&gt;&lt;/constructor-arg&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">        注：&lt;constructor-arg&gt;&lt;/constructor-arg&gt;标签的作用：如果不加该标签则默认使用的是无参构造创建对象，而不是有参构造创建对象。</span><br></pre></td></tr></table></figure>

<h4 id="3-p名称空间注入（本质还是set方法注入属性）"><a href="#3-p名称空间注入（本质还是set方法注入属性）" class="headerlink" title="3.p名称空间注入（本质还是set方法注入属性）"></a>3.p名称空间注入（本质还是set方法注入属性）</h4><p>（1）使用p名称空间注入，可以简化基于xml配置方式。</p>
<p>第一步：在配置文件中添加p名称空间</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">       xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">        <span class="comment">//在配置文件里加入p名称空间</span></span><br><span class="line">       xmlns:p=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="line">       xsi:schemaLocation=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span>&gt;</span><br></pre></td></tr></table></figure>

<p>第二步 进行属性注入，在 bean 标签里面进行操作。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">2</span> set方法注入属性--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;book&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.Book&quot;</span> p:bname=<span class="string">&quot;九阳神功&quot;</span> p:bauthor=<span class="string">&quot;无名氏&quot;</span>&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">        底层还是使用set方法注入属性，但是对set方法注入进行了一定的简化。</span><br></pre></td></tr></table></figure>

<h3 id="Ⅳ-IOC操作Bean管理（xml注入其他类型属性）"><a href="#Ⅳ-IOC操作Bean管理（xml注入其他类型属性）" class="headerlink" title="Ⅳ.IOC操作Bean管理（xml注入其他类型属性）"></a>Ⅳ.IOC操作Bean管理（xml注入其他类型属性）</h3><h4 id="1-字面量"><a href="#1-字面量" class="headerlink" title="1.字面量"></a>1.字面量</h4><p>字面量就是为属性设置一个固定值，例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> String bname = <span class="string">&quot;&quot;</span>;也可以使用&lt;property&gt;&lt;/property&gt;标签来设置字面量。</span><br></pre></td></tr></table></figure>

<p>（1）设置属性值时设置为空值null。使用null标签（只是说明可以设置为null值，实际上引用类型默认为null）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--设置<span class="keyword">null</span>值--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;address&quot;</span> &gt;</span><br><span class="line">            &lt;<span class="keyword">null</span>&gt;&lt;/<span class="keyword">null</span>&gt;</span><br><span class="line">        &lt;/property&gt;</span><br></pre></td></tr></table></figure>

<p>输出结果为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">com.atguigu.Spring5.Book@<span class="number">4802796d</span></span><br><span class="line">易筋经::达摩老祖::<span class="keyword">null</span>             可见空值属性已经设置成功了。</span><br></pre></td></tr></table></figure>



<p>（2）属性值包含特殊符号。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--属性值包含特殊符号</span><br><span class="line">            <span class="number">1</span> 把&lt;&gt;进行转义 &amp;lt; &amp;gt;</span><br><span class="line">            <span class="number">2</span> 把带特殊符号内容写到CDATA</span><br><span class="line">        --&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;address&quot;</span>&gt;</span><br><span class="line">            &lt;value&gt;&lt;![CDATA[&lt;&lt;南京&gt;&gt;]]&gt;&lt;/value&gt;</span><br><span class="line">        &lt;/property&gt;</span><br></pre></td></tr></table></figure>

<p>输出结果为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">com.atguigu.Spring5.Book@65fb9ffc</span><br><span class="line">易筋经::达摩老祖::&lt;&lt;南京&gt;&gt;</span><br></pre></td></tr></table></figure>

<h4 id="2-注入属性-外部-bean"><a href="#2-注入属性-外部-bean" class="headerlink" title="2.注入属性-外部 bean"></a>2.注入属性-外部 bean</h4><p>（1）创建两个类 service 类和 dao 类 。</p>
<p>（2）在 service 调用 dao 里面的方法。 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//在Spring中做到</span></span><br><span class="line">    <span class="comment">//创建UserDao类属性,生成set方法</span></span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserDao</span><span class="params">(UserDao userDao)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.userDao = userDao;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Service add...........&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//原始方式</span></span><br><span class="line">        <span class="comment">//创建userDao对象</span></span><br><span class="line">        UserDao userDao = <span class="keyword">new</span> UserDaoImpl();</span><br><span class="line">        userDao.update();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>（3）在 spring 配置文件中进行配置。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">1</span> service和dao对象创建--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;userService&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.service.UserService&quot;</span>&gt;</span><br><span class="line">        &lt;!--注入userDao对象</span><br><span class="line">            name属性：类里面属性名称</span><br><span class="line">            ref属性：创建userDao对象bean标签id值</span><br><span class="line">        --&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;userDao&quot;</span> ref=<span class="string">&quot;userDaoImpl&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">        &lt;!--注意接口不能<span class="keyword">new</span>对象，应该找他的实现类--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;userDaoImpl&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.dao.UserDaoImpl&quot;</span>&gt;&lt;/bean&gt;</span><br></pre></td></tr></table></figure>



<h4 id="3-注入属性-内部-bean"><a href="#3-注入属性-内部-bean" class="headerlink" title="3.注入属性-内部 bean"></a>3.注入属性-内部 bean</h4><p>（1）一对多关系：部门和员工</p>
<p>一个部门里有多个员工，一个员工属于一个部门。部门是一，员工是多。</p>
<p>（2）实体类之间便是一对多关系，员工表示所属部门，使用对象类型属性进行表示。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//部门类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Dept</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String dname;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDname</span><span class="params">(String dname)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.dname = dname;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//员工类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Emp</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String ename;</span><br><span class="line">    <span class="keyword">private</span> String gender;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//表示员工属于某一个部门，适用对象形式表示</span></span><br><span class="line">    <span class="keyword">private</span> Dept dept;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDept</span><span class="params">(Dept dept)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.dept = dept;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEname</span><span class="params">(String ename)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.ename = ename;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setGender</span><span class="params">(String gender)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.gender = gender;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（3）在 spring 配置文件中进行配置.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--内部bean，即在一个bean里可以再嵌套另一个对象--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;emp&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Emp&quot;</span>&gt;</span><br><span class="line">        &lt;!--设置两个普通属性--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;ename&quot;</span> value=<span class="string">&quot;lucy&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;gender&quot;</span> value=<span class="string">&quot;女&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;!--设置对象类型属性--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;dept&quot;</span>&gt;</span><br><span class="line">            &lt;bean id=<span class="string">&quot;dept&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Dept&quot;</span>&gt;</span><br><span class="line">                &lt;property name=<span class="string">&quot;dname&quot;</span> value=<span class="string">&quot;安保部&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">            &lt;/bean&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h4 id="4-注入属性-级联赋值"><a href="#4-注入属性-级联赋值" class="headerlink" title="4.注入属性-级联赋值"></a>4.注入属性-级联赋值</h4><p>（1）第一种写法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--级联赋值--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;emp&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Emp&quot;</span>&gt;</span><br><span class="line">        &lt;!--设置两个普通属性--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;ename&quot;</span> value=<span class="string">&quot;lucy&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;gender&quot;</span> value=<span class="string">&quot;女&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;!--级联赋值--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;dept&quot;</span> ref=<span class="string">&quot;dept&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;dept&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Dept&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;dname&quot;</span> value=<span class="string">&quot;财务部&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<p>（2）第二种写法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--级联赋值--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;emp&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Emp&quot;</span>&gt;</span><br><span class="line">        &lt;!--设置两个普通属性--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;ename&quot;</span> value=<span class="string">&quot;lucy&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;gender&quot;</span> value=<span class="string">&quot;女&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        &lt;!--级联赋值--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;dept&quot;</span> ref=<span class="string">&quot;dept&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">        <span class="comment">//注意要使用dept.dname需要在Emp类中写get方法，否则会报错</span></span><br><span class="line">        &lt;property name=<span class="string">&quot;dept.dname&quot;</span> value=<span class="string">&quot;技术部&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;dept&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Dept&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;dname&quot;</span> value=<span class="string">&quot;财务部&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h3 id="Ⅴ-IOC-操作-Bean-管理（xml-注入集合属性）"><a href="#Ⅴ-IOC-操作-Bean-管理（xml-注入集合属性）" class="headerlink" title="Ⅴ.IOC 操作 Bean 管理（xml 注入集合属性）"></a>Ⅴ.IOC 操作 Bean 管理（xml 注入集合属性）</h3><h4 id="1、注入数组类型属性"><a href="#1、注入数组类型属性" class="headerlink" title="1、注入数组类型属性"></a>1、注入数组类型属性</h4><h4 id="2、注入-List-集合类型属性"><a href="#2、注入-List-集合类型属性" class="headerlink" title="2、注入 List 集合类型属性"></a>2、注入 List 集合类型属性</h4><h4 id="3、注入-Map-集合类型属性"><a href="#3、注入-Map-集合类型属性" class="headerlink" title="3、注入 Map 集合类型属性"></a>3、注入 Map 集合类型属性</h4><p>（1）创建类，定义数组、list、map、set 类型属性，生成对应 set 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Stu</span> </span>&#123;</span><br><span class="line">    <span class="comment">//1 数组类型属性</span></span><br><span class="line">    <span class="keyword">private</span> String[] courses;</span><br><span class="line">    <span class="comment">//2 list集合类型属性</span></span><br><span class="line">    <span class="keyword">private</span> List&lt;String&gt; list;</span><br><span class="line">    <span class="comment">//3 map集合类型属性</span></span><br><span class="line">    <span class="keyword">private</span> Map&lt;String,String&gt; maps;</span><br><span class="line">    <span class="comment">//4 set集合类型属性</span></span><br><span class="line">    <span class="keyword">private</span> Set&lt;String&gt; sets;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//学生所学多门课程</span></span><br><span class="line">    <span class="keyword">private</span> List&lt;Course&gt; courseList;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCourseList</span><span class="params">(List&lt;Course&gt; courseList)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.courseList = courseList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSets</span><span class="params">(Set&lt;String&gt; sets)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.sets = sets;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCourses</span><span class="params">(String[] courses)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.courses = courses;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setList</span><span class="params">(List&lt;String&gt; list)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.list = list;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setMaps</span><span class="params">(Map&lt;String, String&gt; maps)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.maps = maps;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(Arrays.toString(courses));</span><br><span class="line">        System.out.println(list);</span><br><span class="line">        System.out.println(maps);</span><br><span class="line">        System.out.println(sets);</span><br><span class="line">        System.out.println(courseList);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（2）在 spring 配置文件进行配置。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">1</span> 集合类型属性注入--&gt;</span><br><span class="line">    &lt;!--首先用bean标签创建对象--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;stu&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Stu&quot;</span>&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;!--数组类型属性注入--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;courses&quot;</span>&gt;</span><br><span class="line">            &lt;array&gt;</span><br><span class="line">                &lt;value&gt;java课程&lt;/value&gt;</span><br><span class="line">                &lt;value&gt;数据库课程&lt;/value&gt;</span><br><span class="line">            &lt;/array&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;!--list类型属性注入--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;list&quot;</span>&gt;</span><br><span class="line">            &lt;list&gt;</span><br><span class="line">                &lt;value&gt;张三&lt;/value&gt;</span><br><span class="line">                &lt;value&gt;小三&lt;/value&gt;</span><br><span class="line">            &lt;/list&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;!--map类型属性注入--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;maps&quot;</span>&gt;</span><br><span class="line">            &lt;map&gt;</span><br><span class="line">                &lt;entry key=<span class="string">&quot;JAVA&quot;</span> value=<span class="string">&quot;java&quot;</span>&gt;&lt;/entry&gt;</span><br><span class="line">                &lt;entry key=<span class="string">&quot;PHP&quot;</span> value=<span class="string">&quot;php&quot;</span>&gt;&lt;/entry&gt;</span><br><span class="line">            &lt;/map&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;!--set类型属性注入--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;sets&quot;</span>&gt;</span><br><span class="line">            &lt;set&gt;</span><br><span class="line">                &lt;value&gt;MySQL&lt;/value&gt;</span><br><span class="line">                &lt;value&gt;Redis&lt;/value&gt;</span><br><span class="line">            &lt;/set&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;!--注入list集合类型，值是对象--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;courseList&quot;</span>&gt;</span><br><span class="line">            &lt;list&gt;</span><br><span class="line">                &lt;ref bean=<span class="string">&quot;course1&quot;</span>&gt;&lt;/ref&gt;</span><br><span class="line">                &lt;ref bean=<span class="string">&quot;course2&quot;</span>&gt;&lt;/ref&gt;</span><br><span class="line">            &lt;/list&gt;</span><br><span class="line">        &lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--创建多个course对象--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;course1&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Course&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;cname&quot;</span> value=<span class="string">&quot;Spring5框架&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;course2&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Course&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;cname&quot;</span> value=<span class="string">&quot;MyBatis框架&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h4 id="4、在集合里面设置对象类型值"><a href="#4、在集合里面设置对象类型值" class="headerlink" title="4、在集合里面设置对象类型值"></a>4、在集合里面设置对象类型值</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--创建多个course对象--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;course1&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Course&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;cname&quot;</span> value=<span class="string">&quot;Spring5框架&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;course2&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Course&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;cname&quot;</span> value=<span class="string">&quot;MyBatis框架&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--注入list集合类型，值是对象--&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;courseList&quot;</span>&gt;</span><br><span class="line">            &lt;list&gt;</span><br><span class="line">                &lt;ref bean=<span class="string">&quot;course1&quot;</span>&gt;&lt;/ref&gt;</span><br><span class="line">                &lt;ref bean=<span class="string">&quot;course2&quot;</span>&gt;&lt;/ref&gt;</span><br><span class="line">            &lt;/list&gt;</span><br><span class="line">        &lt;/property&gt;</span><br></pre></td></tr></table></figure>



<h4 id="5、把集合注入部分提取出来"><a href="#5、把集合注入部分提取出来" class="headerlink" title="5、把集合注入部分提取出来"></a>5、把集合注入部分提取出来</h4><p>(1）在 spring 配置文件中引入名称空间 util</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">       xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">       xmlns:p=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="line">       xmlns:util=<span class="string">&quot;http://www.springframework.org/schema/util&quot;</span></span><br><span class="line">       xsi:schemaLocation=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="line"><span class="string">                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd&quot;</span>&gt;</span><br></pre></td></tr></table></figure>

<p>（2）使用 util 标签完成 list 集合注入提取</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">1</span> 提取list集合类型属性注入--&gt;</span><br><span class="line">    &lt;util:list id=<span class="string">&quot;bookList&quot;</span>&gt;</span><br><span class="line">        &lt;value&gt;易筋经&lt;/value&gt;</span><br><span class="line">        &lt;value&gt;九阴真经&lt;/value&gt;</span><br><span class="line">        &lt;value&gt;九阳神功&lt;/value&gt;</span><br><span class="line">    &lt;/util:list&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--<span class="number">2</span> 提取list集合类型属性注入使用--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;book&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Book&quot;</span> scope=<span class="string">&quot;prototype&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;list&quot;</span> ref=<span class="string">&quot;bookList&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h3 id="Ⅵ-IOC-操作-Bean-管理（FactoryBean）"><a href="#Ⅵ-IOC-操作-Bean-管理（FactoryBean）" class="headerlink" title="Ⅵ.IOC 操作 Bean 管理（FactoryBean）"></a>Ⅵ.IOC 操作 Bean 管理（FactoryBean）</h3><h4 id="1、Spring-有两种类型-bean，一种普通-bean，另外一种工厂-bean（FactoryBean）"><a href="#1、Spring-有两种类型-bean，一种普通-bean，另外一种工厂-bean（FactoryBean）" class="headerlink" title="1、Spring 有两种类型 bean，一种普通 bean，另外一种工厂 bean（FactoryBean）"></a>1、Spring 有两种类型 bean，一种普通 bean，另外一种工厂 bean（FactoryBean）</h4><h4 id="2、普通-bean：在配置文件中定义-bean-类型就是返回类型"><a href="#2、普通-bean：在配置文件中定义-bean-类型就是返回类型" class="headerlink" title="2、普通 bean：在配置文件中定义 bean 类型就是返回类型"></a>2、普通 bean：在配置文件中定义 bean 类型就是返回类型</h4><h4 id="3、工厂-bean：在配置文件定义-bean-类型可以和返回类型不一样"><a href="#3、工厂-bean：在配置文件定义-bean-类型可以和返回类型不一样" class="headerlink" title="3、工厂 bean：在配置文件定义 bean 类型可以和返回类型不一样"></a>3、工厂 bean：在配置文件定义 bean 类型可以和返回类型不一样</h4><ul>
<li><p>第一步 创建类，让这个类作为工厂 bean，实现接口 FactoryBean</p>
</li>
<li><p>第二步 实现接口里面的方法，在实现的方法中定义返回的 bean 类型</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBean</span> <span class="keyword">implements</span> <span class="title">FactoryBean</span>&lt;<span class="title">Course</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义返回bean</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Course <span class="title">getObject</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        Course course = <span class="keyword">new</span> Course();</span><br><span class="line">        course.setCname(<span class="string">&quot;abc&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> course;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Class&lt;?&gt; getObjectType() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isSingleton</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ApplicationContext context =</span><br><span class="line">                <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;bean3.xml&quot;</span>);</span><br><span class="line">        <span class="comment">//factoryBean体现：定义的是MyBean类型的对象，返回的是Course类型的对象</span></span><br><span class="line">        Course course = context.getBean(<span class="string">&quot;myBean&quot;</span>, Course.class);</span><br><span class="line">        System.out.println(course);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="Ⅶ-IOC-操作-Bean-管理（bean-作用域）-重点"><a href="#Ⅶ-IOC-操作-Bean-管理（bean-作用域）-重点" class="headerlink" title="Ⅶ.IOC 操作 Bean 管理（bean 作用域）==重点=="></a>Ⅶ.IOC 操作 Bean 管理（bean 作用域）==重点==</h3><h4 id="1、在-Spring-里面，设置创建-bean-实例是单实例还是多实例"><a href="#1、在-Spring-里面，设置创建-bean-实例是单实例还是多实例" class="headerlink" title="1、在 Spring 里面，设置创建 bean 实例是单实例还是多实例"></a>1、在 Spring 里面，设置创建 bean 实例是单实例还是多实例</h4><h4 id="2、在-Spring-里面，默认情况下，bean-是单实例对象"><a href="#2、在-Spring-里面，默认情况下，bean-是单实例对象" class="headerlink" title="2、在 Spring 里面，默认情况下，bean 是单实例对象"></a>2、在 Spring 里面，默认情况下，bean 是单实例对象</h4><p>现在对其进行验证，创建两个不同的Book对象，输出他们的地址，若地址相同则说明是单实例，若地址不同说明是多实例。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testCollection2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ApplicationContext context =</span><br><span class="line">                <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;bean2.xml&quot;</span>);</span><br><span class="line">        Book book1 = context.getBean(<span class="string">&quot;book&quot;</span>, Book.class);</span><br><span class="line">        Book book2 = context.getBean(<span class="string">&quot;book&quot;</span>, Book.class);</span><br><span class="line">       <span class="comment">// book.test();</span></span><br><span class="line">        System.out.println(book1);</span><br><span class="line">        System.out.println(book2);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>输出结果为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">com.atguigu.Spring5.collectionType.Book@6200f9cb</span><br><span class="line">com.atguigu.Spring5.collectionType.Book@6200f9cb</span><br></pre></td></tr></table></figure>

<p>可见两个对象的地址相同，说明默认bean是单实例对象。</p>
<h4 id="3、如何设置单实例还是多实例"><a href="#3、如何设置单实例还是多实例" class="headerlink" title="3、如何设置单实例还是多实例"></a>3、如何设置单实例还是多实例</h4><p>（1）在 spring 配置文件 bean 标签里面有属性（scope）用于设置单实例还是多实例 </p>
<p>（2）scope 属性值 </p>
<ul>
<li><p>第一个值 默认值，singleton，表示是单实例对象 </p>
</li>
<li><p>第二个值 prototype，表示是多实例对象</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--<span class="number">2</span> 提取list集合类型属性注入使用--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;book&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.collectionType.Book&quot;</span> scope=<span class="string">&quot;prototype&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;list&quot;</span> ref=<span class="string">&quot;bookList&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<p>测试结果为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">com.atguigu.Spring5.collectionType.Book@6200f9cb</span><br><span class="line">com.atguigu.Spring5.collectionType.Book@2002fc1d</span><br></pre></td></tr></table></figure>

<p>（3）singleton 和 prototype 区别 </p>
<ul>
<li><p>第一 singleton 单实例，prototype 多实例 </p>
</li>
<li><p>==第二 当设置 scope 值是 singleton的时候，加载 spring 配置文件时候就会创建单实例对象，即==</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ApplicationContext context =</span><br><span class="line">                <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;bean2.xml&quot;</span>);</span><br></pre></td></tr></table></figure>



<ul>
<li>==而当设置 scope 值是 prototype的时候，不是在加载 spring 配置文件时候创建对象，在调用 getBean 方法时候创建多实例对象。==即</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Book book1 = context.getBean(<span class="string">&quot;book&quot;</span>, Book.class);</span><br><span class="line">        Book book2 = context.getBean(<span class="string">&quot;book&quot;</span>, Book.class);</span><br></pre></td></tr></table></figure>

<h3 id="Ⅷ-IOC-操作-Bean-管理（bean-生命周期）-重点"><a href="#Ⅷ-IOC-操作-Bean-管理（bean-生命周期）-重点" class="headerlink" title="Ⅷ.IOC 操作 Bean 管理（bean 生命周期）==重点=="></a>Ⅷ.IOC 操作 Bean 管理（bean 生命周期）==重点==</h3><h4 id="1、生命周期"><a href="#1、生命周期" class="headerlink" title="1、生命周期"></a>1、生命周期</h4><p>（1）从对象创建到对象销毁的过程</p>
<h4 id="2、bean-生命周期"><a href="#2、bean-生命周期" class="headerlink" title="2、bean 生命周期"></a>2、bean 生命周期</h4><p>（1）通过构造器创建 bean 实例（无参数构造） </p>
<p>（2）为 bean 的属性设置值和对其他 bean 引用（调用 set 方法） </p>
<p>（3）调用 bean 的初始化的方法（需要进行配置初始化的方法）</p>
<p>（4）bean 可以使用了（对象获取到了） </p>
<p>（5）当容器关闭时候，调用 bean 的销毁的方法（需要进行配置销毁的方法）</p>
<h4 id="3、演示-bean-生命周期"><a href="#3、演示-bean-生命周期" class="headerlink" title="3、演示 bean 生命周期"></a>3、演示 bean 生命周期</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Orders</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//无参数构造</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Orders</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;第一步 执行无参数构造创建bean实例&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String oname;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setOname</span><span class="params">(String oname)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.oname = oname;</span><br><span class="line">        System.out.println(<span class="string">&quot;第二步 调用set方法设置属性值&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建执行的初始化的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">initMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;第三步 执行初始化的方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建执行的销毁的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroyMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;第五步 执行销毁的方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>相关配置文件：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">       xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">       xmlns:p=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="line">       xmlns:util=<span class="string">&quot;http://www.springframework.org/schema/util&quot;</span></span><br><span class="line">       xsi:schemaLocation=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="line"><span class="string">                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd&quot;</span>&gt;</span><br><span class="line"></span><br><span class="line">    &lt;bean id=<span class="string">&quot;orders&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.Orders&quot;</span> init-method=<span class="string">&quot;initMethod&quot;</span> destroy-method=<span class="string">&quot;destroyMethod&quot;</span>&gt;</span><br><span class="line">        &lt;property name=<span class="string">&quot;oname&quot;</span> value=<span class="string">&quot;手机&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br><span class="line">    </span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure>

<h4 id="4、bean-的后置处理器，bean-生命周期有七步"><a href="#4、bean-的后置处理器，bean-生命周期有七步" class="headerlink" title="4、bean 的后置处理器，bean 生命周期有七步"></a>4、bean 的后置处理器，bean 生命周期有七步</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">（<span class="number">1</span>）通过构造器创建 bean 实例（无参数构造） </span><br><span class="line"></span><br><span class="line">（<span class="number">2</span>）为 bean 的属性设置值和对其他 bean 引用（调用 set 方法） </span><br><span class="line"></span><br><span class="line">（<span class="number">3</span>）把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization  </span><br><span class="line"></span><br><span class="line">（<span class="number">4</span>）调用 bean 的初始化的方法（需要进行配置初始化的方法）</span><br><span class="line"></span><br><span class="line">（<span class="number">5</span>）把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization </span><br><span class="line"></span><br><span class="line">（<span class="number">6</span>）bean 可以使用了（对象获取到了） </span><br><span class="line"></span><br><span class="line">（<span class="number">7</span>）当容器关闭时候，调用 bean 的销毁的方法（需要进行配置销毁的方法）</span><br></pre></td></tr></table></figure>



<h4 id="5、演示添加后置处理器效果"><a href="#5、演示添加后置处理器效果" class="headerlink" title="5、演示添加后置处理器效果"></a>5、演示添加后置处理器效果</h4><p>（1）创建类，实现接口 BeanPostProcessor，创建后置处理器。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBeanPost</span> <span class="keyword">implements</span> <span class="title">BeanPostProcessor</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessBeforeInitialization</span><span class="params">(Object bean, String beanName)</span> <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;在初始化之前执行的方法&quot;</span>);</span><br><span class="line">        <span class="comment">//bean就是创建出来的实例对象</span></span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">postProcessAfterInitialization</span><span class="params">(Object bean, String beanName)</span> <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;在初始化之后执行的方法&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;!--配置后置处理器--&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;myBeanPost&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.bean.MyBeanPost&quot;</span>&gt;&lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<p>执行结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">第一步 执行无参数构造创建bean实例</span><br><span class="line">第二步 调用set方法设置属性值</span><br><span class="line">在初始化之前执行的方法</span><br><span class="line">第三步 执行初始化的方法</span><br><span class="line">在初始化之后执行的方法</span><br><span class="line">第四步 获取创建bean实例对象</span><br><span class="line">com.atguigu.Spring5.bean.Orders@<span class="number">28701274</span></span><br><span class="line">第五步 执行销毁的方法</span><br></pre></td></tr></table></figure>

<h3 id="Ⅸ-IOC-操作-Bean-管理（xml-自动装配）"><a href="#Ⅸ-IOC-操作-Bean-管理（xml-自动装配）" class="headerlink" title="Ⅸ.IOC 操作 Bean 管理（xml 自动装配）"></a>Ⅸ.IOC 操作 Bean 管理（xml 自动装配）</h3><h4 id="1、什么是自动装配"><a href="#1、什么是自动装配" class="headerlink" title="1、什么是自动装配"></a>1、什么是自动装配</h4><p>即根据指定装配规则（属性名称或者属性类型），Spring 自动将匹配的属性值进行注入</p>
<h4 id="2、演示自动装配过程"><a href="#2、演示自动装配过程" class="headerlink" title="2、演示自动装配过程"></a>2、演示自动装配过程</h4><p>（1）根据属性名称自动注入。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--实现自动装配</span><br><span class="line">        bean标签属性autowire，配置自动装配</span><br><span class="line">        autowire属性常用两个值：</span><br><span class="line">            byName根据属性名称注入 ，注入值bean的id值和类属性名称一样</span><br><span class="line">            byType根据属性类型注入</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;emp&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.autowire.Emp&quot;</span> autowire=<span class="string">&quot;byName&quot;</span>&gt;</span><br><span class="line">        &lt;!--&lt;property name=<span class="string">&quot;dept&quot;</span> ref=<span class="string">&quot;dept&quot;</span>&gt;&lt;/property&gt;--&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<p>（2）根据属性类型自动注入.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--实现自动装配</span><br><span class="line">        bean标签属性autowire，配置自动装配</span><br><span class="line">        autowire属性常用两个值：</span><br><span class="line">            byName根据属性名称注入 ，注入值bean的id值和类属性名称一样</span><br><span class="line">            byType根据属性类型注入,需要注意的是当相同类型的bean定义多个会报错，此时只能用               byName</span><br><span class="line">    --&gt;</span><br><span class="line">    &lt;bean id=<span class="string">&quot;emp&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.atguigu.Spring5.autowire.Emp&quot;</span> autowire=<span class="string">&quot;byType&quot;</span>&gt;</span><br><span class="line">        &lt;!--&lt;property name=<span class="string">&quot;dept&quot;</span> ref=<span class="string">&quot;dept&quot;</span>&gt;&lt;/property&gt;--&gt;</span><br><span class="line">    &lt;/bean&gt;</span><br></pre></td></tr></table></figure>

<h3 id="Ⅹ-IOC-操作-Bean-管理-外部属性文件"><a href="#Ⅹ-IOC-操作-Bean-管理-外部属性文件" class="headerlink" title="Ⅹ.IOC 操作 Bean 管理(外部属性文件)"></a>Ⅹ.IOC 操作 Bean 管理(外部属性文件)</h3><h4 id="1、直接配置数据库信息"><a href="#1、直接配置数据库信息" class="headerlink" title="1、直接配置数据库信息"></a>1、直接配置数据库信息</h4><p>（1）配置德鲁伊druid连接池 </p>
<p>（2）引入德鲁伊连接池依赖 jar 包</p>
<h4 id="2、引入外部属性文件配置数据库连接池"><a href="#2、引入外部属性文件配置数据库连接池" class="headerlink" title="2、引入外部属性文件配置数据库连接池"></a>2、引入外部属性文件配置数据库连接池</h4><p>（1）创建外部属性文件，properties 格式文件，写数据库信息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">prop.driverClass=com.mysql.jdbc.Driver</span><br><span class="line">prop.url=jdbc:mysql:<span class="comment">//localhost:3306/userDb</span></span><br><span class="line">prop.userName=root</span><br><span class="line">prop.password=root</span><br></pre></td></tr></table></figure>

<p>（2）把外部 properties 属性文件引入到 spring 配置文件中</p>
<ul>
<li>引入 context 名称空间 </li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="line">&lt;beans xmlns=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="line">       xmlns:xsi=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="line">       xmlns:p=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="line">       xmlns:util=<span class="string">&quot;http://www.springframework.org/schema/util&quot;</span></span><br><span class="line">       xmlns:context=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="line">       xsi:schemaLocation=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="line"><span class="string">                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd</span></span><br><span class="line"><span class="string">                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;/beans&gt;</span><br></pre></td></tr></table></figure>

<p>需要着重注意的配置： <font color=red>xmlns:context=”<a target="_blank" rel="noopener" href="http://www.springframework.org/schema/context&quot;">http://www.springframework.org/schema/context&quot;</a></font></p>
<p><a target="_blank" rel="noopener" href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a> <a target="_blank" rel="noopener" href="http://www.springframework.org/schema/context/spring-context.xsd&quot;">http://www.springframework.org/schema/context/spring-context.xsd&quot;</a></p>
<ul>
<li>在 spring 配置文件使用标签引入外部属性文件</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--引入外部属性文件--&gt;</span><br><span class="line">   &lt;context:property-placeholder location=<span class="string">&quot;classpath:jdbc.properties&quot;</span>/&gt;</span><br><span class="line"></span><br><span class="line">   &lt;!--配置连接池--&gt;</span><br><span class="line">   &lt;bean id=<span class="string">&quot;dataSource&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;com.alibaba.druid.pool.DruidDataSource&quot;</span>&gt;</span><br><span class="line">       &lt;property name=<span class="string">&quot;driverClassName&quot;</span> value=<span class="string">&quot;$&#123;prop.driverClass&#125;&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">       &lt;property name=<span class="string">&quot;url&quot;</span> value=<span class="string">&quot;$&#123;prop.url&#125;&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">       &lt;property name=<span class="string">&quot;username&quot;</span> value=<span class="string">&quot;$&#123;prop.userName&#125;&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">       &lt;property name=<span class="string">&quot;password&quot;</span> value=<span class="string">&quot;$&#123;prop.password&#125;&quot;</span>&gt;&lt;/property&gt;</span><br><span class="line">   &lt;/bean&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="Ⅺ-IOC-操作-Bean-管理-基于注解方式"><a href="#Ⅺ-IOC-操作-Bean-管理-基于注解方式" class="headerlink" title="Ⅺ.IOC 操作 Bean 管理(基于注解方式)"></a>Ⅺ.IOC 操作 Bean 管理(基于注解方式)</h3><h4 id="1、什么是注解"><a href="#1、什么是注解" class="headerlink" title="1、什么是注解"></a>1、什么是注解</h4><p> （1）注解是代码特殊标记，格式：@注解名称(属性名称=属性值, 属性名称=属性值..) </p>
<p>（2）使用注解，注解可以作用在类上面，方法上面，属性上面 </p>
<p>（3）使用注解目的：简化 xml 配置，使用更优雅的方式实现配置。</p>
<h4 id="2、Spring-针对-Bean-管理中创建对象提供注解"><a href="#2、Spring-针对-Bean-管理中创建对象提供注解" class="headerlink" title="2、Spring 针对 Bean 管理中创建对象提供注解"></a>2、Spring 针对 Bean 管理中创建对象提供注解</h4><p>（1）@Component </p>
<p>（2）@Service </p>
<p>（3）@Controller </p>
<p>（4）@Repository </p>
<p>上面四个注解功能是一样的，都可以用来创建 bean 实例</p>
<h4 id="3、基于注解方式实现对象创建"><a href="#3、基于注解方式实现对象创建" class="headerlink" title="3、基于注解方式实现对象创建"></a>3、基于注解方式实现对象创建</h4><p>第一步 引入依赖</p>
<p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/image-20210406163000273.png" alt="image-20210406163000273"></p>
<p>第二步 开启组件扫描：告诉Spring容器现在在哪个类里面加入注解，然后去扫描这个类。不开启的话Spring不知道要做什么事情，也不知道扫描哪个包中的哪个类。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:p</span>=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--开启组件扫描</span></span><br><span class="line"><span class="comment">        1 如果扫描多个包，多个包使用逗号隔开</span></span><br><span class="line"><span class="comment">        &lt;context:component-scan base-package=&quot;com.atguigu.Spring5,com.atguigu.Spring5.service&quot;&gt;&lt;/context:component-scan&gt;</span></span><br><span class="line"><span class="comment">        2 扫描包上层目录</span></span><br><span class="line"><span class="comment">        &lt;context:component-scan base-package=&quot;com.atguigu&quot;&gt;&lt;/context:component-scan&gt;</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.atguigu.Spring5,com.atguigu.Spring5.service&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>第三步 创建类，在类上面添加创建对象注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在注解里面value属性值可以省略不写，</span></span><br><span class="line"><span class="comment">//默认值是类名称，首字母小写  UserService -- userService</span></span><br><span class="line"><span class="comment">//@Component(value = &quot;userService&quot;)  //&lt;bean id=&quot;userService&quot; class=&quot;..&quot;/&gt;</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(value = &quot;abc&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;service add.......&quot;</span>+name);</span><br><span class="line">        userDao.add();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4、开启组件扫描细节配置"><a href="#4、开启组件扫描细节配置" class="headerlink" title="4、开启组件扫描细节配置"></a>4、开启组件扫描细节配置</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--示例1</span></span><br><span class="line"><span class="comment">    use-default-filters=&quot;false&quot; 表示现在不使用默认 filter，而使用自己配置的 filter</span></span><br><span class="line"><span class="comment">    context:include-filter ，设置扫描哪些内容</span></span><br><span class="line"><span class="comment">    Controller：表示在com.atguigu包下只扫描带@Controller注解的类</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.atguigu&quot;</span> <span class="attr">use-default-filters</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:include-filter</span> <span class="attr">type</span>=<span class="string">&quot;annotation&quot;</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">expression</span>=<span class="string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--示例 2</span></span><br><span class="line"><span class="comment">         下面配置扫描包所有内容</span></span><br><span class="line"><span class="comment">         context:exclude-filter： 设置哪些内容不进行扫描</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.atguigu&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context:exclude-filter</span> <span class="attr">type</span>=<span class="string">&quot;annotation&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">expression</span>=<span class="string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="5、基于注解方式实现属性注入"><a href="#5、基于注解方式实现属性注入" class="headerlink" title="5、基于注解方式实现属性注入"></a>5、基于注解方式实现属性注入</h4><p>（1）@Autowired：根据属性类型进行自动装配 </p>
<ul>
<li><p>第一步 把 service 和 dao 对象创建，在 service 和 dao 类添加创建对象注解 </p>
</li>
<li><p>第二步 在 service 注入 dao 对象，在 service 类添加 dao 类型属性，在属性上面使用注解.</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"> 	<span class="comment">//定义 dao 类型属性</span></span><br><span class="line"> 	<span class="comment">//不需要添加 set 方法</span></span><br><span class="line"> 	<span class="comment">//添加注入属性注解</span></span><br><span class="line"> 	<span class="meta">@Autowired</span> </span><br><span class="line"> 	<span class="keyword">private</span> UserDao userDao;</span><br><span class="line"> 	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">()</span> </span>&#123;</span><br><span class="line"> 	System.out.println(<span class="string">&quot;service add.......&quot;</span>);</span><br><span class="line"> 	userDao.add();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>（2）@Qualifier：根据名称进行注入，这个 @Qualifier 注解要和上面 @Autowired 一起使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义 dao 类型属性</span></span><br><span class="line"><span class="comment">//不需要添加 set 方法</span></span><br><span class="line"><span class="comment">//添加注入属性注解</span></span><br><span class="line"><span class="meta">@Autowired</span> <span class="comment">//根据类型进行注入</span></span><br><span class="line"><span class="meta">@Qualifier(value = &quot;userDaoImpl1&quot;)</span> <span class="comment">//根据名称进行注入</span></span><br><span class="line"><span class="keyword">private</span> UserDao userDao;</span><br></pre></td></tr></table></figure>



<p>（3）@Resource：可以根据类型注入，也可以根据名称注入。注意：Resourse注解是javax.annotation.Resource包中的，因此尽量使用前面的方法注入属性。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Resource</span>  <span class="comment">//根据类型进行注入</span></span><br><span class="line"><span class="meta">@Resource(name = &quot;userDaoImpl1&quot;)</span>  <span class="comment">//根据名称进行注入</span></span><br><span class="line"><span class="keyword">private</span> UserDao userDao;</span><br></pre></td></tr></table></figure>



<p>（4）@Value：注入普通类型属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">@Value(value = &quot;abc&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"><span class="comment">//输出为：</span></span><br><span class="line">service add.......abc</span><br><span class="line">dao add.....</span><br></pre></td></tr></table></figure>

<h4 id="6-完全注解开发"><a href="#6-完全注解开发" class="headerlink" title="6.完全注解开发"></a>6.完全注解开发</h4><p>（1）创建配置类，替代 xml 配置文件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.Spring5.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.ComponentScan;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span>  <span class="comment">//作为配置类，替代xml配置文件</span></span><br><span class="line"><span class="meta">@ComponentScan(basePackages = &#123;&quot;com.atguigu&quot;&#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（2）编写测试类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testService2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//加载配置类</span></span><br><span class="line">        ApplicationContext context</span><br><span class="line">                = <span class="keyword">new</span> AnnotationConfigApplicationContext(SpringConfig.class);</span><br><span class="line">        UserService userService = context.getBean(<span class="string">&quot;userService&quot;</span>, UserService.class);</span><br><span class="line">        System.out.println(userService);</span><br><span class="line">        userService.add();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Spring/" rel="tag"># Spring</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/04/Servlet%EF%BC%8C%E6%88%91%E8%BF%98%E6%98%AF%E7%88%B1%E4%BD%A0%E7%9A%84/" rel="prev" title="Servlet，我还是爱你的">
                  <i class="fa fa-chevron-left"></i> Servlet，我还是爱你的
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/04/Spring5%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0(%E4%B8%89)/" rel="next" title="Spring5框架学习系列（三）">
                  Spring5框架学习系列（三） <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">AiLaodu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/algoliasearch@4.10.5/dist/algoliasearch-lite.umd.js" integrity="sha256-HWlivbjXc58GuU4EIZziqIE83FFZ/da42de13pGZnMA=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4.30.2/dist/instantsearch.production.min.js" integrity="sha256-eqhx/eer5fsD7YooSb21wsfJaQkJ/4gF3bmRBZP7q2o=" crossorigin="anonymous"></script><script src="/js/third-party/search/algolia-search.js"></script>





  





</body>
</html>
